package com.rzj.sys_lab_assistant.controller;

import com.rzj.sys_lab_assistant.entity.AssistantRepair;
import com.rzj.sys_lab_assistant.entity.bo.AssistantBO;
import com.rzj.sys_lab_assistant.service.IAssistantRepairService;
import com.rzj.sys_lab_assistant.service.IExportService;
import com.rzj.sys_lab_assistant.utils.ResultsUtils;
import com.rzj.sys_lab_assistant.utils.TimeUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.Map;

/**
 * @program: sys_lab_assistant
 * @description:
 * @author: 作者
 * @create: 2021-07-02 22:10
 */
@Controller
@RequestMapping("assistantRepair")
public class AssistantRepairController {

    @Autowired
    IAssistantRepairService assistantRepairService;
    @Autowired
    IExportService exportService;

    ResultsUtils resultsUtils;

    TimeUtils timeUtils;

    @RequestMapping("toAdd")
    public String toAdd() {
        return "assistantRepair/assistantRepairAdd";
    }

    @RequestMapping("toEdit/{repairId}")
    public String toEdit(Model model, @PathVariable("repairId") int repairId) {
        model.addAttribute("repair", assistantRepairService.getAssistantRepairById(repairId));
        return "assistantRepair/assistantRepairUpdate";
    }

    @RequestMapping("assistantRepairList")
    public String assistantRepairList(Model model, HttpSession session) { // 助理列表数据
        AssistantBO assistant = (AssistantBO) session.getAttribute("assistant");
        model.addAttribute("repairList", assistantRepairService.listAssistantRepairs(assistant.getAId())); // 根据助理登录session
        return "assistantRepair/assistantRepairList";
    }

    @RequestMapping("assistantRepairByLabList/{labId}")
    public String assistantRepairByLabList(Model model, @PathVariable("labId") int labId) { // 技术员列表数据 根据实验室查询本实验室报修情况
        model.addAttribute("repairList", assistantRepairService.listAssistantRepairsByLab(labId));
        return "assistantRepair/assistantRepairByLabList";
    }

    @RequestMapping("assistantRepairAdd")
    @ResponseBody
    public Map<String, String> assistantRepairAdd(@RequestParam("equipmentName") String equipmentName,
                                                  @RequestParam("equipmentNo") String equipmentNo,
                                                  @RequestParam("errorContent") String errorContent,
                                                  @RequestParam("repairTime") String repairTime, HttpSession session) throws ParseException {
        AssistantBO assistant = (AssistantBO) session.getAttribute("assistant");
        AssistantRepair assistantRepair = new AssistantRepair(0, equipmentName, equipmentNo, errorContent,
                timeUtils.getDate(repairTime), assistant.getAId()); // 根据session获值
        int flag = assistantRepairService.saveAssistantRepair(assistantRepair);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "添加成功！");
        } else {
            return resultsUtils.resultsMap("error", "添加失败，请联系管理员！");
        }
    }

    @RequestMapping("assistantRepairEdit")
    @ResponseBody
    public Map<String, String> assistantRepairEdit(@RequestParam("repairId") String repairId,
                                                   @RequestParam("equipmentName") String equipmentName,
                                                   @RequestParam("equipmentNo") String equipmentNo,
                                                   @RequestParam("errorContent") String errorContent,
                                                   @RequestParam("repairTime") String repairTime, HttpSession session) throws ParseException {
        AssistantBO assistant = (AssistantBO) session.getAttribute("assistant");
        AssistantRepair assistantRepair = new AssistantRepair(Integer.valueOf(repairId), equipmentName, equipmentNo, errorContent,
                timeUtils.getDate(repairTime), assistant.getAId()); // 根据session获值
        int flag = assistantRepairService.updateAssistantRepair(assistantRepair);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "修改成功！");
        } else {
            return resultsUtils.resultsMap("error", "修改失败，请联系管理员！");
        }
    }

    @RequestMapping("assistantRepairDel")
    @ResponseBody
    public Map<String, String> assistantRepairDel(@RequestParam("repairId") String repairId) {
        int flag = assistantRepairService.deleteAssistantRepair(Integer.valueOf(repairId));
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "删除成功！");
        } else {
            return resultsUtils.resultsMap("error", "删除失败，请联系管理员！");
        }
    }

    @RequestMapping("repairExport/{repairId}")
    public Map<String, String> repairExport(@PathVariable("repairId") int repairId, HttpServletRequest request,
                                            HttpServletResponse response) throws IOException, IllegalAccessException {
        exportService.downloadMedicalRecord(repairId, request, response);
        return resultsUtils.resultsMap("success", "导出成功！");
    }


}
